$(function () {
    //获取页面id
    var machineId = T.p("machineId");
    console.log("获取机器id:" + machineId);
    vm.p.machineId=machineId;
    var pageNum = T.p("pageNum");
    console.log("pageNum:" + pageNum);
    vm.p.pageNum=pageNum;


    //初始化时间
    var initStartTime = moment().startOf('day');
    vm.initDatePicker(initStartTime);
    vm.q.time = initStartTime.format('YYYY-MM-DD');

    //初始化检测类型列表
    vm.getMachineCheckList(machineId);

    vm.getColModel(machineId);
    console.log("q:" + JSON.stringify(vm.q));
});

var vm = new Vue({

    el: '#rrapp',
    data: {
        q: {
            time: null,
            machineCheckId: null
        },
        p: {
            machineId: null,
            pageNum: null
        },
        caption: null,
        colModel: [],
        showList: true,
        checkTypeList: []
    },
    methods: {
        query: function () {
            vm.reload();
        },
        reload: function (event) {
            vm.showList = true;
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    "time": vm.q.time,
                    "machineCheckId": vm.q.machineCheckId
                }
            }).trigger("reloadGrid");
        },
        getColModel: function (macineId) {
            $.get(baseURL + "machine/data/getColNamesAndModel/" + macineId, function (r) {
                if (r.code === 0) {
                    var jqGridEntity = r.jqGridEntity;
                    vm.colModel = jqGridEntity.colModelEntity;
                    vm.caption = jqGridEntity.machineEntity.name;
                    vm.initJqGridData(macineId);
                } else {
                    alert(r.msg);
                }
            });
        },
        initDatePicker: function (start) {
            $('#datetimes').daterangepicker({
                "singleDatePicker": true,
                "showDropdowns": true,
                "linkedCalendars": false,
                "showCustomRangeLabel": false,
                "startDate": start,
                "opens": "center",
                "locale": {
                    "format": "YYYY-MM-DD",
                    "applyLabel": '确定',
                    "cancelLabel": '取消',
                    "daysOfWeek": ['日', '一', '二', '三', '四', '五', '六'],
                    "monthNames": ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
                    "firstDay": 1
                }
            }, function (start, end, label) {
                console.log('New date range selected: ' + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD') + ' (predefined range: ' + label + ')');
                vm.q.time = end.format('YYYY-MM-DD');
                console.log("vm中的q:" + JSON.stringify(vm.q));
            });
        },

        initJqGridData: function (machineId) {
            console.log("colModel:" + JSON.stringify(vm.colModel));
            $("#jqGrid").jqGrid({
                url: baseURL + 'machine/data/check',
                datatype: "json",
                mtype: "GET",
                postData: {
                    "machineId": machineId,
                    "time": vm.q.time,
                    "machineCheckId": vm.q.machineCheckId
                },
                caption: vm.caption + "-首检/巡检",
                colModel: vm.colModel,
                height: '100%',
                rownumbers: true, //设置列表显示序号,需要注意在colModel中不能使用rn作为index
                rownumWidth: 50, //设置显示序号的宽度，默认为25
                autowidth: true,
                jsonReader: {
                    root: "checkData",
                    repeatitems: false
                },
                gridComplete: function () {
                    //隐藏grid底部滚动条
                    $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
                },
                loadComplete: function (xhr) {
                    if (xhr.code != 0) {
                        alert(xhr.msg);
                    }
                }
            });
        },
        goBack: function () {
            console.log("返回机器列表")
            // window.history.back(-1);
            // window.location.href = 'machine.html?machineId=' + id + '';
            window.location.href = 'machine.html?machineId='+vm.p.machineId+'&pageNum='+vm.p.pageNum+'';
        },
        getMachineCheckList: function (machineId) {
            $.get(baseURL + "machine/data/checkTypeList/" + machineId, function (r) {
                if (r.code === 0) {
                    vm.checkTypeList = r.machineCheckTypeList;
                    vm.q.machineCheckId= r.machineCheckTypeList[0].id;
                } else {
                    alert(r.msg);
                }
            });
        }
    }
});